package io.github.luminion.sqlbooster.extension.pagehelper;

import com.github.pagehelper.PageInfo;
import io.github.luminion.sqlbooster.core.BoosterPage;
import io.github.luminion.sqlbooster.util.ReflectUtils;
import lombok.RequiredArgsConstructor;

import java.util.List;

/**
 * PageHelper 分页适配对象
 *
 * @param <T> 记录的类型
 * @author luminion
 * @since 1.0.0
 */
@RequiredArgsConstructor
public class PageHelperPage<T> implements BoosterPage<T> {

    /**
     * PageHelper 的分页对象
     */
    private final PageInfo<T> pageInfo;

    @Override
    public List<T> getRecords() {
        return pageInfo.getList();
    }

    @Override
    public long getTotal() {
        return pageInfo.getTotal();
    }

    @Override
    public long getCurrent() {
        return pageInfo.getPageNum();
    }

    @Override
    public long getSize() {
        return pageInfo.getSize();
    }

    @Override
    public <R> BoosterPage<R> convertRecords(Class<R> targetType) {
        PageInfo<R> convert = pageInfo.convert(e -> ReflectUtils.toTarget(e, targetType));
        return new PageHelperPage<>(convert);
    }
}
